<?php if(!defined('IN_FIRADIO')) {header('HTTP/1.1 403 Forbidden');exit('<h1>Access Denied</h1>');}?>
<?php $GLOBALS['script'][] = function() {?><script>
vue.data.dbchar = 'a';
vue.data.disabled = false;
vue.data.button = {};
vue.data.button.down = false;
vue.data.button.del = false;
vue.data.api_recordcount = {};
vue.data.config = {};
vue.data.config.autoreload = {};
vue.data.config.autoreload.interval = 5000;
vue.data.canbefetch = {};
if (typeof(vue.watch) !== 'object') vue.watch = {};
vue.watch['config.autoreload.interval'] = function () {
    clearInterval(vue.data.config.autoreload.obj);
    if (vue.data.config.autoreload.interval > 0) {
        vue.data.config.autoreload.obj = setInterval(function () {
            vue.data.form.is_autoreload = 1;
            vue.methods.submit_bnb_recordcount();
        }, vue.data.config.autoreload.interval);
    }
};

var submit_after_result = function (that) {
    that.disabled = false;
};

vue.methods.formatBytes = window.formatBytes;
vue.methods.submit_bnb_recordcount = function () {
    const that = this;
    const form = window.clone(vue.data.form);
    vue.data.form.is_autoreload = 0;
    that.disabled = true;
    that.ApiPost('/panel/email/account_recordcount.php', form).then(function (data) {
        vue.data.api_recordcount = data.data;
        vue.data.canbefetch = {};
        for (var k in data.data.canbefetch) {
            const state = data.data.canbefetch[k];
            vue.data.canbefetch[state] = true;
        }
    }, function (data) {
        console.log('reject:', data);
    }).catch(function (reason) {
        console.log('catch:', reason);
    })
};
vue.methods.getRecordCount = function (db, status) {
    const status_id = status['id'];
    const data = (typeof(db[status_id]) === 'object') ? db[status_id] : {};
    const count = (typeof(data.count) === 'number') ? data.count : 0;
    return count;
};
vue.methods.sum_db_status_list = function (db, list) {
    var ret = 0;
    for (var status_i in list) {
        const status_id = list[status_i];
        const data = (typeof(db[status_id]) === 'object') ? db[status_id] : {};
        const count = (typeof(data.count) === 'number') ? data.count : 0;
        ret += count;
    }
    return ret;
};
vue.methods.get_status_list = function (list) {
    var ret = [];
    for (var status_i in list) {
        const status = list[status_i];
        ret.push(status.id);
    }
    return ret;
};
vue.methods.sum_db_all = function (db) {
    var ret = 0;
    for (var status_id in db) {
        const data = (typeof(db[status_id]) === 'object') ? db[status_id] : {};
        const count = (typeof(data.count) === 'number') ? data.count : 0;
        ret += count;
    }
    return ret;
};
vue.methods.write_to_file = function (db, hostid, dbchar, status_id, downType) {
    if (downType == 2) {
        db[status_id].loading2 = true;
    } else {
        db[status_id].loading = true;
    }
    const that = this;
    //alert(hostid + '----' + dbchar  + '----' + status_id);
    const form = {};
    form.hostid = hostid;
    form.dbchar = dbchar;
    form.status_id = status_id;
    form.downType = downType;
    that.ApiPost('/panel/email/write_to_file.php', form).then(function (data) {
        vue.methods.submit_bnb_recordcount();
    }, function (data) {
        vue.methods.submit_bnb_recordcount();
        console.log('reject:', data);
    }).catch(function (reason) {
        vue.methods.submit_bnb_recordcount();
        console.log('catch:', reason);
    })
};
vue.methods.getFilesize = function (db, status_id, downType) {
    const data = (typeof(db[status_id]) === 'object') ? db[status_id] : {};
    return ((downType==2)?data.filesize2:data.filesize);
};
vue.methods.getFileDown = function (hostid, dbchar, status_id, downType) {
    const http_pre = '/api/download/';
    const filename_prefix = (downType == 2) ? 'bnb_record2_' : 'bnb_record_';
    return http_pre + '/' + filename_prefix + hostid + '_' + dbchar + '_' + status_id + '.txt';
};
vue.methods.delFile = function (hostid, dbchar, status_id, downType) {
    if (!confirm("确定要删除生成的文件吗？")) return;
    const that = this;
    //alert(hostid + '----' + dbchar  + '----' + status_id + '----' + downType);
    const form = {};
    form.hostid = hostid;
    form.dbchar = dbchar;
    form.status_id = status_id;
    form.downType = downType;
    that.ApiPost('/panel/email/download_file_del.php', form).then(function (data) {
        vue.methods.submit_bnb_recordcount();
    }, function (data) {
        vue.methods.submit_bnb_recordcount();
        console.log('reject:', data);
    }).catch(function (reason) {
        vue.methods.submit_bnb_recordcount();
        console.log('catch:', reason);
    })
};
vue.methods.delDb = function (hostid, dbchar, status_id, rcount) {
    if (!confirm("确定要从数据库中删除吗？")) return;
    const that = this;
    //alert(hostid + '----' + dbchar  + '----' + status_id + '----' + rcount);
    const form = {};
    form.hostid = hostid;
    form.dbchar = dbchar;
    form.status_id = status_id;
    form.rcount = rcount;
    that.ApiPost('/panel/email/record_del_db.php', form).then(function (data) {
        vue.methods.submit_bnb_recordcount();
    }, function (data) {
        vue.methods.submit_bnb_recordcount();
        console.log('reject:', data);
    }).catch(function (reason) {
        vue.methods.submit_bnb_recordcount();
        console.log('catch:', reason);
    })
};


vue.methods.submit_canbefetch = function () {
    const that = this;
    setTimeout(function () {
        const stateList = [];
        for (var state in that.canbefetch) {
            if (that.canbefetch[state]) stateList.push(state);
        }
        //console.log(stateList.join(','));
        const form = {};
        form.stateList = stateList;
        that.ApiPost('/panel/email/canbefetch-set.php', form).then(function (data) {
        }, function (data) {
            console.log('reject:', data);
        }).catch(function (reason) {
            console.log('catch:', reason);
        })
    }, 0);
};

vue.mounted = function () {
    vue.methods.submit_bnb_recordcount();
    vue.watch['config.autoreload.interval']();
};
</script><?php }?>

<!-- Custom Tabs -->
<form v-on:submit="submit_bnb_recordcount" onsubmit="return false" :disabled="disabled">
<div class="nav-tabs-custom">
    
    <ul class="nav nav-tabs">
        <li class="pull-left" style="font-size: 14px; padding: 8px;">
            <select v-model="config.autoreload.interval" style="margin-left: 10px;">
                <option value="1000">自动刷新/1s</option>
                <option value="3000">自动刷新/3s</option>
                <option value="5000">自动刷新/5s</option>
                <option value="0">手动刷新</option>
            </select>
            <a href="javascript:void(0)" v-on:click="form={};submit_bnb_recordcount();"
            class="fa fa-fw fa-refresh" style="font-size: 16px; display: inline;"></a>
        </li>
    </ul>

    <div class="tab-content">
        <div class="tab-pane active">
            <div class="row">
                <div class="col-xs-12 col-md-6 col-lg-4">
                    <table class="table table-bordered table-striped dataTable">
                        <tr align="center">
                            <td>状态</td>
                            <td>数量</td>
                            <td>启用</td>
                            <td>下载</td>
                            <td>删除</td>
                        </tr>
                        <tr align="center" v-for="row of api_recordcount.rows">
                            <td>{{row['state']}}</td>
                            <td>{{row['count1']}}</td>
                            <td><input type="checkbox" v-model="canbefetch[row['state']]"
                            v-on:change="submit_canbefetch()"
                            ></input></td>
                            <td>下载</td>
                            <td>删除</td>
                        </tr>

                    </table>

                </div>
            </div>
            <!-- /.tab-content -->
        </div>
        <!-- nav-tabs-custom -->
    </div>
    <!-- /.col -->

</div>
</form>
